<?php


class QuestionBankService extends Service
{
  /**
   * @param string $className
   * @return QuestionBankService
   */
  public static function instance($className = __CLASS__)
  {
    return parent::instance($className);
  }

  /**
   * 获取题库列表
   * @param int $pageSize 分页大小
   * @param bool $with 是否关联
   * @param bool $showAll 是否显示所有
   * @param int $user_id 创建者
   * @param string $keywords 搜索
   * @return array
   */
  public function getList($pageSize = 20, $with = true, $showAll = false, $user_id = 0, $keywords = '')
  {
    $criteria = new CDbCriteria();
    //显示某用户的
    if ($user_id > 0) {
      $criteria->condition = 'user_id = :user_id';
      $criteria->params = array(':user_id' => $user_id);
    }
    //是否显示所有
    if (!$showAll) {
      $criteria->addCondition('question_bank.state = 1');
    }
    //关键字
    if (!empty($keywords)) {
      $criteria->addSearchCondition('question_bank.name', $keywords);
    }
    //关联
    if ($with) {
      $criteria->with = array(
          'questions',
          'user'
      );
    }
    $criteria->order = 'question_bank.bank_id desc';
    $count = QuestionBank::model()->count($criteria);
    $pages = new CPagination($count);
    $pages->pageSize = $pageSize;
    $pages->applyLimit($criteria);
    $criteria->offset = $pages->currentPage * $pages->pageSize;
    $list = QuestionBank::model()->findAll($criteria);
    return array(
        'list' => $list,
        'pages' => $pages
    );
  }

  /**
   * 添加/编辑题库
   * @param array $data 题库数据
   * @param int $user_id 创建者ID(编辑时凭证)
   * @return array|bool|string
   */
  public function post($data = array(), $user_id = 0)
  {
    if (empty($data['bank_id'])) {
      $question_bank = new QuestionBank();
    } else {
      $question_bank = QuestionBank::model()->findByPk($data['bank_id']);
      if ($question_bank->user_id != $user_id) {
        return '对不起，该课程不是你创建的!';
      }
    }
    $question_bank->attributes = $data;
    return $question_bank->save() ? true : $question_bank->errors;
  }

  /**
   * 设置题库状态
   * @param array $list
   * @param int $state
   * @return bool
   */
  public function setState($list = array(), $state = 0)
  {
    $criteria = new CDbCriteria();
    $criteria->addInCondition('bank_id', $list);
    return QuestionBank::model()->updateAll(array('state' => $state), $criteria) ? true : false;
  }

  /**
   * 获取试卷
   * @param int $bank_id
   * @return mixed
   */
  public function getExam($bank_id = 0)
  {
    $key = 'bank-' . $bank_id;
    $data = Yii::app()->cache->get($key);
    if ($data === false) {
      $criteria = new CDbCriteria();
      $criteria->condition = 'question_bank.bank_id=:bank_id and question_bank.state = 1';
      $criteria->params = array(':bank_id' => $bank_id);
      $criteria->with = array(
          'questions',
          'user'
      );
      $list = QuestionBank::model()->find($criteria);
      Yii::app()->cache->set($key, $list, 3600 * 24 * 7);
    }
    return $data;
  }

  /**
   * 检测试题
   * @param int $bank_id
   * @return bool|string
   */
  public function checkExam($bank_id = 0)
  {
    if ($bank_id > 0) {
      if (QuestionBank::model()->findByAttributes(array('bank_id' => $bank_id, 'state' => 1))) {
        return true;
      } else {
        return '试题不存在!';
      }
    } else {
      return '试题不存在!';
    }
  }

  /**
   * 获得考试成绩
   * @param $bank_id
   * @param array $answer
   * @return array|int
   */
  public function getResult($bank_id, $answer = array())
  {
    //分割question
    $data = array();
    foreach ($answer as $key => $row) {
      $keys = explode('_', $key);
      $data[$keys[1]] = $row;
    }
    $score = 0;
    $per = intval(100 / count($data));
    //判断正确还是错误
    foreach ($data as $k => $v) {
      //查询正确答案
      $question = Question::model()->findByPk($k);
      if ($question->answer == $v) {
        $score += $per;
      }
    }
    $model = new Score();
    $model->attributes = array(
        'score' => $score,
        'bank_id' => $bank_id
    );
    return $model->save() ? $score : $model->errors;
  }

  /**
   * 获取学生做过的题库
   * @param int $user_id
   * @param string $keywords
   * @param int $pageSize
   * @return array
   */
  public function getStudentQuestionBank($user_id = 0, $keywords = '',$pageSize=20)
  {
    $criteria = new CDbCriteria();
    $criteria->condition='score.user_id=:user_id';
    $criteria->params = array(':user_id'=>$user_id);
    $count = Score::model()->count($criteria);
    $pages = new CPagination($count);
    $pages->pageSize = $pageSize;
    $pages->applyLimit($criteria);
    $criteria->order = 'score.created_at desc';
    if(!empty($keywords)){
      $criteria->addSearchCondition('question_bank.name',$keywords);
    }
    $criteria->with = array(
      'user',
      'bank'
    );
    $criteria->offset = $pages->currentPage*$pages->pageSize;
    $criteria->limit = $pageSize;
    $list = Score::model()->findAll($criteria);
    return array(
      'list'=>$list,
      'pages'=>$pages
    );
  }
} 